package com.aaa.os.service.impl;

import com.aaa.common.constant.ConstantUtil;
import com.aaa.common.entity.Pay;
import com.aaa.common.entity.ProductOrder;
import com.aaa.common.entity.UserIntegral;
import com.aaa.common.entity.UserIntegralLog;
import com.aaa.common.util.HttpClient;
import com.aaa.os.service.PayService;
import com.aaa.os.service.RemoteUserIntegralLogService;
import com.aaa.os.service.RemoteUserIntegralService;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class PayServiceImpl implements PayService {
    /**
     * 获取微信支付状态
     * @param orderNo
     * @return
     */
    @Override
    public Map getPayStatus(String orderNo) {
        try {
            Map paramMap = new HashMap();
            //参数
            paramMap.put("appid", ConstantUtil.PayConfig.APPID);
            paramMap.put("mch_id", ConstantUtil.PayConfig.MCH_ID);//"1558950191"
            paramMap.put("out_trade_no", orderNo+"");
            //1.00    1
            //m.put("total_fee", new BigDecimal(order.getPrice()*order.getNum()).multiply(new BigDecimal("100")).longValue() + ""); //单位 分
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            //m.put("sign", "127.0.0.1");
            //m.put("trade_type", "NATIVE");
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantUtil.PayConfig.API_KEY));
            client.setHttps(true);
            client.post();
            String xml = client.getContent();//xml
            System.out.println("支付后的信息========" + xml);
            //System.out.println("支付后的信息========" + xml);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            System.out.println("支付之后的信息的内容==================="+resultMap);
            return resultMap;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取微信支付二维码
     * @param productOrder
     * @return
     */
    @Override
    public Map getNativeByOrder(ProductOrder productOrder) {
        log.info("总金额："+productOrder.getTotalAmount());
        log.info("用户ID："+productOrder.getUserId());
        log.info("门店ID："+productOrder.getStoreId());
        log.info("message："+productOrder.getMessage());
        // 2.3 设置商品描述（示例：使用门店ID和详情ID）
        String describe = "门店[" + productOrder.getStoreId() + "]-订单[" + productOrder.getOrderNo() + "]";
        productOrder.setMessage(describe);
        try {
            //统一下单   生成预支付的订单
            Map paramMap = new HashMap();
            //参数
            paramMap.put("appid", ConstantUtil.PayConfig.APPID); // 修复参数名（原APP_ID改为"appid"）
            paramMap.put("mch_id", ConstantUtil.PayConfig.MCH_ID);//"1558950191"
            paramMap.put("out_trade_no", productOrder.getOrderNo());
            //1.00    1
            paramMap.put("total_fee", new BigDecimal(productOrder.getTotalAmount()).multiply(new BigDecimal("100")).longValue() + ""); //单位 分
            paramMap.put("body", productOrder.getMessage());
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("spbill_create_ip", "127.0.0.1");
            paramMap.put("trade_type", "NATIVE");
            paramMap.put("notify_url", "http://www.chenkaixiang.top/notifyWeiXinPay");
            //统一下单
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantUtil.PayConfig.API_KEY));
            client.setHttps(true);
            client.post();
            //生成的预支付订单的消息
            String xml = client.getContent();//xml
            System.out.println("-------------" + xml);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            System.out.println(resultMap);
            Map returnMap = new HashMap<>();
            //map.put("out_trade_no", orederNum);
            //map.put("course_id", 1);
            //map.put("total_fee", 1 + "");
            //map.put("result_code", resultMap.get("result_code"));
            returnMap.put("code_url", resultMap.get("code_url"));
            return returnMap;
        } catch (Exception e) {
            // System.out.println(e.getMessage());
            log.error(e.getMessage());
        }
        return null;
    }
}
